const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
  
  },
  onLoad(){
    
  },

  onShow(){
    this.getMyMessage();
  },
  onHide(){
    this.RedMessage({
      read_all: true
    });
  },

  /*分享*/
  onShareAppMessage(){
    
  },
  
  /*初始数据*/
  InitData(){
    this.setData({
      showNoData: false
    });
  },

  /**
   * @explain 我的消息
   * @param thirdCookie 用户标识
   * */ 
   getMyMessage(){
     wx.showNavigationBarLoading();
     let _this = this;
     _this.InitData();
     SERVICE.POST(SERVICE.URL.myMessage,{
       thirdCookie: wx.getStorageSync("thirdCookie")
     },(status,res)=>{
       wx.hideNavigationBarLoading();
       if(res.data.status === "success"){
         let list = res.data.message_list;
         console.log("消息列表：",list);
         if(list.length > 0){
           _this.setData({
             list: res.data.message_list
           });
         }else{
           _this.setData({
             showNoData: true,
             list: null
           });
         }
       }else{
         wx.showToast({
           title: "获取信息失败",
           icon: "none",
           mask: true
         });
       }
     });
   },
   
  /**
   * @explain clear 清除消息
   * @param thirdCookie
   * */  
   clear(e){
     let _this =this, arr = [], msg_list = e.currentTarget.dataset.msg;
     for(let i = 0;i < msg_list.length; i++){
       arr.push(msg_list[i].id);
     }
     wx.showModal({
       title: "提示",
       content: "是否清空消息",
       success: function(res){
         if(res.confirm){
           wx.showLoading({
             title: "清空中...",
             mask: true
           })
           SERVICE.POST(SERVICE.URL.delMymessage,{
             thirdCookie: wx.getStorageSync("thirdCookie"),
             id: JSON.stringify(arr)
           },(status,res)=>{
             wx.hideLoading();
             if (res.data.status === "success"){
               _this.getMyMessage();
             }else{
               wx.showToast({
                 title: "清空失败",
                 icon: "none",
                 mask: true,
                 duration: 2000
               });
             }
           });
         }
       }
     })
   },
   getItem(e){
    let get_type = e.currentTarget.dataset.type;
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    if(status == 0 || get_type == "maintenTemplate" || get_type == "claimTemplate"){
      this.RedMessage({
        messageid: id
      },get_type);
    }
   },

   RedMessage(params,getType){
     params.thirdCookie = wx.getStorageSync("thirdCookie");
     let _this = this;     
     wx.showLoading({
       title: "",
       mask: true
     });
     SERVICE.POST(SERVICE.URL.redMessage, params, (status, res) => {
       wx.hideLoading();
       _this.getMyMessage();
       if (getType && getType == "maintenTemplate") {
         wx.navigateTo({
           url: "../my-record/my-record?navItem=0",
         })
       } else if (getType && getType == "claimTemplate") {
         wx.navigateTo({
           url: "../my-record/my-record?navItem=1",
         })
       }
     });
   }
});